package cn.iocoder.yudao.module.student.controller.app.job;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.company.CompanyApi;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyRespDTO;
import cn.iocoder.yudao.module.member.api.job.JobApi;
import cn.iocoder.yudao.module.member.api.job.dto.JobPageReqDTO;
import cn.iocoder.yudao.module.member.api.job.dto.JobRespDTO;
import cn.iocoder.yudao.module.member.api.jobView.JobViewApi;
import cn.iocoder.yudao.module.member.api.jobView.dto.JobViewCreateReqDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 -学生端- 招聘职位")
@RestController
@RequestMapping("/student/job")
@Validated
public class StudentJobController {

    @Resource
    private JobApi jobApi;
    @Resource
    private CompanyApi companyApi;
    @Resource
    private JobViewApi jobViewApi;

    @GetMapping("/get")
    @ApiOperation("获得招聘职位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<JobRespDTO> getJob(@RequestParam("id") Long id) {

        JobRespDTO job = jobApi.getJob(id);
        CompanyRespDTO company=companyApi.getCompanyById(job.getCompanyId());
        job.setCompanyJobCount(jobApi.getCompanyJobCount(job.getCompanyId()));
        job.setIntroduction(company.getIntroduction());
        job.setCompanyLogo(company.getCompanyLogo());
        job.setIndustry(company.getIndustry());
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        if(null!=loginUser){
            JobViewCreateReqDTO createReqDTO=new JobViewCreateReqDTO();
            createReqDTO.setCompanyId(job.getCompanyId());
            createReqDTO.setJobId(id);
            createReqDTO.setStudentId(loginUser.getId());
            jobViewApi.saveJobView(createReqDTO);
        }

        return success(job);
    }



    @GetMapping("/isJob")
    @ApiOperation("获得招聘职位状态")
    public CommonResult<String> isJob(@RequestParam("id") Long id) {
        String flag = jobApi.isJob(id);
        return success(flag);
    }

    @PostMapping("/page")
    @ApiOperation("获得招聘职位分页")
    public CommonResult<PageResult<JobRespDTO>> getJobPage(@RequestBody JobPageReqDTO pageVO) {
        PageResult<JobRespDTO> pageResult =jobApi.getJobPage(pageVO);
        return success(pageResult);
    }
    @PostMapping("/page1")
    @ApiOperation("新获得招聘职位分页")
    public CommonResult<PageResult<JobRespDTO>> getJobPage1(@RequestBody JobPageReqDTO pageVO) {
        PageResult<JobRespDTO> pageResult =jobApi.getJobPage1(pageVO);
        return success(pageResult);
    }

    @PostMapping("/getJobViewPage")
    @ApiOperation("获得学生浏览记录")
    @PreAuthenticated
    public CommonResult<List<JobRespDTO>> getJobViewPage(@RequestBody JobPageReqDTO pageVO) {
        SortingField sortField=new SortingField();
        sortField.setOrder("desc");
        sortField.setField("viewTime");
        pageVO.setSortField(sortField);
        List<JobRespDTO> pageResult =jobApi.getJobViewPage(pageVO);
        return success(pageResult);
    }


    @GetMapping("/getHotJobList")
    @ApiOperation("获得热门职位top50")
    public CommonResult<List<JobRespDTO>> getHotJobList() {
        List<JobRespDTO> pageResult = jobApi.getHotJob();
        return success(pageResult);
    }

    @GetMapping("/getHotJobList1")
    @ApiOperation("新获得热门职位top50")
    public CommonResult<List<JobRespDTO>> getHotJobList1() {
        List<JobRespDTO> pageResult = jobApi.getHotJob1();
        return success(pageResult);
    }

//    @PostMapping("/page")
//    @ApiOperation("获得招聘职位分页")
//    public CommonResult<PageResult<JobRespDTO>> getHotJobPage(@RequestBody JobPageReqDTO pageVO) {
//        PageResult<JobRespDTO> pageResult =jobApi.getHotJobPage(pageVO);
//        return success(pageResult);
//    }


}
